﻿package com.superwindcloud.blackjack

import android.util.Log
import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Box
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.MutableState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.unit.dp


@Composable
fun DetermineTheWinnerOfTheBankerAndPlayer(
    theDealerHasFinishedTakingTheCards: MutableState<Boolean>,
    rememberChipValue: MutableState<Int>,
    rememberRemainingChips: MutableState<Int>,
    dealerInitScore: MutableState<Int>,
    PlayerCurrentScore: MutableState<Int>
) {
    if(!theDealerHasFinishedTakingTheCards.value) return
    val playerWin = remember { mutableStateOf(false) }
    val dealerWin = remember { mutableStateOf(false) }
    if (PlayerCurrentScore.value > dealerInitScore.value) {
        Log.d("DetermineWinner", "Player wins")
        LaunchedEffect(Unit) {
            rememberRemainingChips.value += rememberChipValue.value
        }
        playerWin.value = true
    } else if (PlayerCurrentScore.value < dealerInitScore.value) {
        Log.d("DetermineWinner", "Dealer wins")
        dealerWin.value = true
        LaunchedEffect(Unit) {
            rememberRemainingChips.value -= rememberChipValue.value
        }

    } else {
        Log.d("DetermineWinner", "Tie")
        playerWin.value = false
        dealerWin.value = false
    }

    VictorySettlementInterface(playerWin , dealerWin)

}
@Composable
fun VictorySettlementInterface(playerWin: MutableState<Boolean>, dealerWin: MutableState<Boolean>) {
     if ( playerWin.value) {

         Box(modifier = Modifier.fillMaxSize()) {
             Image(
                 painter = painterResource(id = R.drawable.point_user_win),
                 contentDescription = "lose",
                 modifier = Modifier
                     .size(200.dp)
                     .align(Alignment.Center)
                     .padding(top = 100.dp)
             )
         }
     }
    if ( dealerWin.value) {

        Box(modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(id = R.drawable.point_banker_win),
                contentDescription = "lose",
                modifier = Modifier
                    .size(200.dp)
                    .align(Alignment.Center)
                    .padding(top = 100.dp)
            )
        }}
    if (!playerWin.value && !dealerWin.value) {

        Box(modifier = Modifier.fillMaxSize()) {
            Image(
                painter = painterResource(id = R.drawable.point_draw),
                contentDescription = "lose",
                modifier = Modifier
                    .size(200.dp)
                    .align(Alignment.Center)
                    .padding(top = 100.dp)
            )
        }
    }
}
